home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fwcp / src / cpirq.c < prev    next >
Text File  |  1995-02-15  |  6KB  |  309 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <malloc.h>
  4. #include    <stdarg.h>
  5. #include    <string.h>
  6. #include    <errno.h>
  7. #include    <dos.h>
  8. #include    <fcntl.h>
  9. #include    <share.h>
  10. #include    "dir.h"
  11.  
  12. #define TRUE        1
  13. #define FALSE        0
  14. #define ERR        (-1)
  15.  
  16. #define    IO_BUFF_SIZE    (4 * 1024)
  17.  
  18. static    char    *prog_name = "cp";
  19. static    int    force_flg     = TRUE;
  20. static    int    recursive_flg = TRUE;
  21. static    int    verbose_flg   = FALSE;
  22. static    int    update_flg    = FALSE;
  23. static    int    verify_flg    = FALSE;
  24. static    int    check_flg     = TRUE;
  25.  
  26. typedef    struct    _CP {
  27.     struct    _CP    *next;
  28.     char        *src_file;
  29.     char        *dis_file;
  30. } CPTR;
  31.  
  32. static    int        src_fp = EOF, dis_fp = EOF;
  33. static    unsigned long    file_size = 0L;
  34. static    DIRECT        src_stat;
  35. static    CPTR        *copy_top = NULL;
  36. static    char        io_buff[IO_BUFF_SIZE];
  37.  
  38. static    void    message(char *form, ...)
  39. {
  40.     va_list arg;
  41.     char    tmp[128];
  42.  
  43.     va_start(arg, form);
  44.     vsprintf(tmp, form, arg);
  45.     va_end(arg);
  46.  
  47.     BEEP();
  48.     SYSLINE(tmp);
  49.     GETCH();
  50. }
  51. char    *file_name(char *file)
  52. {
  53.     int n;
  54.     static char tmp[42];
  55.  
  56.     if ( (n = strlen(file)) <= 40 )
  57.     return file;
  58.  
  59.     sprintf(tmp, "%-3.3s...%s", file, file + (n - 34));
  60.  
  61.     return tmp;
  62. }
  63. static    int    file_error(char *file)
  64. {
  65.     message("%s : %s", file_name(file), strerror(errno));
  66.     return ERR;
  67. }
  68. int    copy_check()
  69. {
  70.     return (copy_top != NULL ? TRUE : FALSE);
  71. }
  72. int    copy_irq()
  73. {
  74.     int n, i;
  75.     int rc, ch;
  76.     CPTR *cp;
  77.     unsigned read_size, write_size;
  78.     char tmp[128];
  79.  
  80.     if ( copy_top == NULL )
  81.     return FALSE;
  82.  
  83.     if ( src_fp == EOF )
  84.     goto FILEOPEN;
  85.  
  86.     if ( _dos_read(src_fp, io_buff, IO_BUFF_SIZE, &read_size) ) {
  87.     file_error(copy_top->src_file);
  88.     goto ERROR;
  89.     }
  90.  
  91.     if ( read_size == 0 )
  92.     goto ENDOF;
  93.  
  94.     if ( _dos_write(dis_fp, io_buff, read_size, &write_size) ) {
  95.     file_error(copy_top->dis_file);
  96.     goto ERROR;
  97.     }
  98.  
  99.     if ( read_size != write_size ) {
  100.     message("%s : disk full ?", file_name(copy_top->dis_file));
  101.     goto ERROR;
  102.     }
  103.  
  104.     file_size += read_size;
  105.     return FALSE;
  106.  
  107. ENDOF:
  108.  
  109.     if ( src_stat.d_size != file_size ) {
  110.     message("%s : file read error ?", file_name(copy_top->src_file));
  111.     goto ERROR;
  112.     }
  113.  
  114.     if ( _dos_setftime(dis_fp, src_stat.d_date, src_stat.d_time) ) {
  115.     file_error(copy_top->dis_file);
  116.     goto ERROR;
  117.     }
  118.  
  119.     _dos_close(dis_fp);
  120.  
  121. SKIP:
  122.     _dos_close(src_fp);
  123.     src_fp = EOF;
  124.  
  125. NEXT:
  126.     copy_wind_check(copy_top->dis_file);
  127.     cp = copy_top->next;
  128.     free(copy_top->src_file);
  129.     free(copy_top->dis_file);
  130.     free(copy_top);
  131.     copy_top = cp;
  132.     SYSLINE("");
  133.  
  134. FILEOPEN:
  135.     if ( copy_top == NULL )
  136.     return FALSE;
  137.  
  138.     if ( dos_stat(copy_top->src_file, &src_stat) ) {
  139.     file_error(copy_top->src_file);
  140.     goto NEXT;
  141.     }
  142.  
  143.     if ( _dos_open(copy_top->src_file, O_RDONLY, &src_fp) ) {
  144.     file_error(copy_top->src_file);
  145.     goto NEXT;
  146.     }
  147.  
  148.     if ( _dos_creatnew(copy_top->dis_file, src_stat.d_att, &dis_fp) ) {
  149.     file_error(copy_top->dis_file);
  150.     goto SKIP;
  151.     }
  152.  
  153.     file_size = 0L;
  154.  
  155.     i = 0;
  156.     for ( n = 'A' ; n <= 'Z' ; n++ ) {
  157.     for ( cp = copy_top ; cp != NULL ; cp = cp->next ) {
  158.         if ( *(cp->src_file) == n || *(cp->dis_file) == n ) {
  159.         tmp[i++] = n;
  160.         tmp[i++] = ':';
  161.         break;
  162.         }
  163.     }
  164.     }
  165.     tmp[i++] = ' ';
  166.     strcpy(tmp + i, file_name(copy_top->dis_file));
  167.     SYSLINE(tmp);
  168.  
  169.     return FALSE;
  170.  
  171. ERROR:
  172.     _dos_close(src_fp);
  173.     _dos_close(dis_fp);
  174.     src_fp = EOF;
  175.  
  176.     if ( unlink(copy_top->dis_file) )
  177.     file_error(copy_top->dis_file);
  178.  
  179.     goto NEXT;
  180. }
  181. char    *stralloc(char *str)
  182. {
  183.     char *p;
  184.  
  185.     if ( (p = (char *)malloc(strlen(str) + 1)) != NULL )
  186.     strcpy(p, str);
  187.     return p;
  188. }
  189. int    file_copy(char *src_file, char *dis_file)
  190. {
  191.     CPTR *cp, *tp;
  192.  
  193.     if ( (cp = (CPTR *)malloc(sizeof(CPTR))) == NULL ) {
  194.     message("%s : malloc error", file_name(src_file));
  195.     return ERR;
  196.     }
  197.  
  198.     if ( (cp->src_file = stralloc(src_file)) == NULL ) {
  199.     message("%s : malloc error", file_name(src_file));
  200.     free(cp);
  201.     return ERR;
  202.     }
  203.  
  204.     if ( (cp->dis_file = stralloc(dis_file)) == NULL ) {
  205.     message("%s : malloc error", file_name(dis_file));
  206.     free(cp->src_file);
  207.     free(cp);
  208.     return ERR;
  209.     }
  210.  
  211.     cp->next = NULL;
  212.     if ( (tp = copy_top) == NULL )
  213.     copy_top = cp;
  214.     else {
  215.     while ( tp->next != NULL )
  216.         tp = tp->next;
  217.     tp->next = cp;
  218.     }
  219.  
  220.     return FALSE;
  221. }
  222. static    int    chkdir(char *dir)
  223. {
  224.     int ch;
  225.     int rc = 0;
  226.     DIRECT st;
  227.  
  228.     if ( !dos_stat(dir, &st) ) {
  229.     if ( !IS_DIR((&st)) ) {
  230.         message("%s : %s not directory", file_name(dir));
  231.         return ERR;
  232.     }
  233.  
  234.     if ( check_flg ) {
  235.         BEEP();
  236.         for ( ; ; ) {
  237.         verbose("'%s' copy directory %s", file_name(dir),
  238.             (rc == 0 ? "([Yes]/No) ?" : "(Yes/[No]) ?"));
  239.         if ( (ch = yesno(&rc)) == ERR )
  240.             return ERR;
  241.         else if ( ch == TRUE )
  242.             break;
  243.         }
  244.     }
  245.  
  246.     } else {
  247.     if ( mkdir(dir) )
  248.         return file_error(dir);
  249.     }
  250.  
  251.     return FALSE;
  252. }
  253. int    dir_copy(char *src_dir, char *dis_dir)
  254. {
  255.     DIR    *dir;
  256.     DIRECT *dp;
  257.     DIRECT st;
  258.     unsigned long sz;
  259.     char   src_file[128];
  260.     char   dis_file[128];
  261.  
  262.     if ( abort_check() )
  263.     return ERR;
  264.  
  265.     if ( chkdir(dis_dir) )
  266.     return ERR;
  267.  
  268.     if ( (dir = opendir(src_dir)) == NULL )
  269.     return file_error(src_dir);
  270.  
  271.     if ( verbose_flg != FALSE ) {
  272.     sz = 0L;
  273.     while ( (dp = readdir(dir)) != NULL ) {
  274.         if ( dp->d_name[0] == '.' )
  275.             continue;
  276.         if ( !IS_DIR(dp) )
  277.         sz += dp->d_size;
  278.     }
  279.     apend_scale(sz);
  280.     seekdir(dir, 0);
  281.     }
  282.  
  283.     while ( (dp = readdir(dir)) != NULL ) {
  284.     if ( dp->d_name[0] == '.' )
  285.         continue;
  286.  
  287.     strcpy(src_file, path_make(src_dir, dp->d_name));
  288.     strcpy(dis_file, path_make(dis_dir, dp->d_name));
  289.  
  290.     if ( IS_DIR(dp) ) {
  291.         if ( recursive_flg == FALSE )
  292.         continue;
  293.         if ( dir_copy(src_file, dis_file) )
  294.         goto ERROR;
  295.         continue;
  296.     }
  297.  
  298.     if ( file_copy(src_file, dis_file) )
  299.         goto ERROR;
  300.     }
  301.  
  302.     closedir(dir);
  303.     return FALSE;
  304.  
  305. ERROR:
  306.     closedir(dir);
  307.     return ERR;
  308. }
  309.